﻿using UnityEngine;
using System.Collections;

public class GfxNeuronGraphTex : MonoBehaviour {

	public Neuron neuron;
	public Texture2D tex;
	public MeshRenderer renderer;
	public int res = 32;

	void Start () {
		if (renderer == null) renderer = gameObject.GetComponent<MeshRenderer> ();
		if (neuron == null) neuron = gameObject.GetComponent<Neuron> ();
		tex = new Texture2D (res, res, TextureFormat.ARGB32, false);
		for (int x = 0; x < res; x++) {
			for (int y = 0; y < res; y++) {
				tex.SetPixel (x, y, Color.gray);
			}
		}
		tex.Apply ();
		renderer.material.mainTexture = tex;
	}
	
	
	void Update () {
		float xx = (neuron.soma / 65534.0f);
		float yy = (neuron.axon / 65534.0f);
		int vertx =  (int)(xx* res);
		int horzy = (int)(yy* res);
		Color vertc;
		Color horzc;
		{
			float h = (xx > 0.5f) ? 0.33f : 0;
			float v = 1;//(xx-0.5f) * (xx-0.5f);
			vertc = UnityEditor.EditorGUIUtility.HSVToRGB(h, 1, v);
		}
		{
			float h = (yy > 0.5f) ? 0.33f : 0;
			float v = 1;//(yy-0.5f) * (yy-0.5f);
			horzc = UnityEditor.EditorGUIUtility.HSVToRGB(h, 1, v);
		}

		for (int x = 0; x < res; x++) {
			float fx = ((x+0.5f)/res);
			float f = neuron.activationFunctionf(fx);
			for (int y = 0; y < res; y++) {
				Color c = Color.white;
				//int fi = f * res;
				float fy = ((y+0.5f)/res);
				if (fy > f) c = Color.black;
				if (y == horzy) c = horzc;
				if (x == vertx) c = vertc;
				//f = field.getIntensity(transform.localToWorldMatrix * local);
				tex.SetPixel (x, y, c);
			}
		}
		tex.Apply ();
		renderer.material.mainTexture = tex;
		renderer.material.color = Color.white;
	}
}
